package com.ykwep.web.server.data;


import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.users.User;
import com.ykwep.web.server.PMF;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Page implements DataStoreObject{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;
    
    public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
    @NotPersistent
	private String contentDeatached = "";

	@Persistent
    private User author;
    
    @Persistent
    private String title;
    
    @Persistent
    private String name;

    public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Persistent
    private Text content;

    @Persistent
    private Date date;

    public Page(User author, String content, Date date) {
        this.author = author;
        this.content = new Text(content);
        this.date = date;
    }

    public Long getId() {
        return id;
    }

    public User getAuthor() {
        return author;
    }

    public Boolean getPublished() {
		return published;
	}

	public void setPublished(Boolean published) {
		this.published = published;
	}

	@Persistent
    private Boolean published;
    
    public String getContent() {
    	if(content==null)
    		return contentDeatached;
    	else
    		return content.getValue();
    }

    public Date getDate() {
        return date;
    }

    public void setAuthor(User author) {
        this.author = author;
    }

    public void setContent(String content) {
        this.content = new Text(content);
    }

    public void setDate(Date date) {
        this.date = date;
    }
    
    public static List<Page> getLatestPages(int howMany){
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	Query query = pm.newQuery();
    	query.setClass(Page.class);
    	query.setFilter("published==true");
    	query.setOrdering("date desc");
    	query.setRange(0, howMany);
    	List<Page> newsItems = new LinkedList<Page>(pm.detachCopyAll((List<Page>)PMF.preDeatach((List<DataStoreObject>) query.execute())));
    	
    	pm.close();
    	
    	return newsItems;
    }
    
    public static List<Page> getAllPages(){
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	Query query = pm.newQuery();
    	query.setClass(Page.class); 
    	query.setOrdering("date desc");
    	List<Page> newsItems = new LinkedList<Page>(pm.detachCopyAll((List<Page>)PMF.preDeatach((List<DataStoreObject>) query.execute())));
    	pm.close();
    	
    	return newsItems; 
    }
    
    public static List<Page> getAllPublishedPages(){
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	Query query = pm.newQuery();
    	query.setClass(Page.class);
    	query.setFilter("published==true");
    	query.setOrdering("date desc");
    	List<Page> newsItems = new LinkedList<Page>(pm.detachCopyAll((List<Page>)PMF.preDeatach((List<DataStoreObject>) query.execute())));
    	pm.close();
    	
    	return newsItems;
    }
    
    public static Page getPageByName(String pageName){
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	Query query = pm.newQuery();
    	query.setClass(Page.class);
    	query.setFilter("name == pageName && published==true");
    	query.setOrdering("date desc");
    	query.declareParameters("String pageName");
    	List<Page> pages = new LinkedList<Page>(pm.detachCopyAll((List<Page>)PMF.preDeatach((List<DataStoreObject>) query.execute(pageName))));
    	pm.close();
    	
    	return (pages.isEmpty() ? null : pages.get(0));
    }


	@Override
	public void preDeatach() {
		this.contentDeatached = content.getValue();
		
	}
}